package top.fzqblog.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;

import java.util.List;

import top.fzqblog.firstapp.R;
import top.fzqblog.interf.OnImageClickListener;
import top.fzqblog.model.Results;

/**
 * Created by 抽离 on 2017/9/27 0027.
 */

public class XRVAdapter extends RecyclerView.Adapter<ImageViewHolder>{

    private Context context;

    private List<Results> list;

    private OnImageClickListener listener;

    public XRVAdapter(Context context, List<Results> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public ImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.retrofit_glide, parent, false);
        ImageViewHolder imageViewHolder = new ImageViewHolder(view);
        return imageViewHolder;
    }

    @Override
    public void onBindViewHolder(ImageViewHolder holder, int position) {
        Log.i("tag", position+"");
        Glide.with(context)
                .load(list.get(position).getUrl())
                .fitCenter()
                .animate(R.anim.fadein)
                .placeholder(R.mipmap.img_default_meizi)
                .error(R.mipmap.img_default_meizi)
                .into(holder.imageView);

        holder.imageView.setOnClickListener(view -> listener.onClick(position));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void addAll(List<Results> results){
        list.addAll(results);
    }

    public void setOnclickItem(OnImageClickListener listener){
        this.listener = listener;
    }

}

